var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
import moment from "../../utils/moment.js";
const app = getApp()
Page({
    data: {
        tabs: ["完成", "待付款", "待发货", "待收货"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        goods: [],
    },
    onLoad: function() {
        var that = this;
        this.getOrderList(1);
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
            }
        });
    },
    getOrderList(type) {
        wx.showLoading({
            title: '获取订单中',
        })
        const db = wx.cloud.database()
        //获取商品列表
        db.collection('order_list').where({
            buyer: app.globalData.openid,
            status: type
        }).get({
            success: res => {
                let data = this.formatData(res.data)
                this.setData({
                    goods: data
                })
                wx.hideLoading()
            },
            fail: err => {
                wx.showToast({
                    icon: 'none',
                    title: '查询记录失败'
                })
                console.error('[数据库] [查询记录] 失败：', err)
            }
        })
    },
    tabClick: function(e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
        this.getOrderList(parseInt(e.currentTarget.id) + 1);
    },
    formatData(data) {
        data.forEach(item => {
            item.complete_time = moment(item.complete_time).format("YYYY-MM-DD HH:mm:ss")
            item.consignee_time = moment(item.consignee_time).format("YYYY-MM-DD HH:mm:ss")
            item.order_time = moment(item.order_time).format("YYYY-MM-DD HH:mm:ss")
            item.pay_time = moment(item.pay_time).format("YYYY-MM-DD HH:mm:ss")
        })
        return data
    }

});